// 网关配置的前缀
var companyServer = "/companySer"

var currentPage = 1;    // 当前页
var pageSize = 10;      // 页大小
var total = 0;          // 数据总量
var pages = 1;          // 页数总量
var search_value = "";

// let userId = "1354255569179971599";
let userId = "";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        companyServer = data.companyServer;
    });
    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
    getFollowedList(currentPage, pageSize, search_value);
}

function getFollowedList(currentPage, pageSize, search_value) {
    $.ajax({
        type: "GET",
        async: false,
        url: companyServer + "/company/getFollowedList",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            currentPage: currentPage,
            pageSize: pageSize,
            searchValue: search_value,
            userId: userId
        },
        success: function (data) {
            total = data.datas.total;
            pageSize = data.datas.size;
            pages = data.datas.pages;

            //清空数据
            $("#followedList").empty();
            //返回的数据用data.rows获取内容
            $.each(data.datas.records, function (i, item) {
                let sendMessA = "";
                if (item.postNumber > 0) {
                    sendMessA = "<a href='/company/companyDetailsPage/" + item.companyId + "#jobsList'>" +
                        "<i class='lnr lnr-envelope'></i>" +
                        "<span>投简历</span>" +
                        "</a>"

                } else {
                    sendMessA = "<a style='pointer-events: none;'>" +
                        "<i class='lnr lnr-cross'></i>" +
                        "<span>暂无岗位</span>" +
                        "</a>"
                }

                let companyStatus = "";
                if (item.companyStatus === "正常") {
                    companyStatus = "<span class='approved'>正常</span>";
                } else if (item.companyStatus === "暂停") {
                    companyStatus = "<span class='pending'>暂停</span>";
                } else if (item.companyStatus === "停止") {
                    companyStatus = "<span class='rejected'>停止</span>";
                }

                $("#followedList").append(
                    "<tr class='job-application-item' id='com-" + item.companyId + "'>" +
                    "<td class='application-job'>" +
                    "<h3><a onclick='companyDetail(\"" + item.companyId + "\")'>" + item.companyName + "</a></h3>" +
                    "</td>" +

                    "<td class='application-location'>" +
                    "<span> " + item.companyAddress + " </span>" +
                    "</td>" +

                    "<td class='application-categories'>" +
                    "<span> " + item.industry + " </span>" +
                    "</td>" +

                    "<td class='open-job'>" +
                    "<a>" + item.postNumber + "&nbsp;个空缺职位</a>" +
                    "</td>" +

                    "<td class='application-contact'>" +
                    sendMessA +
                    "</td>" +

                    "<td class='status'>" +
                    companyStatus +
                    "</td>" +

                    "<td class='delete-application text-right'>" +
                    "<a onclick='cancelFollowCompany(\"" + item.companyId + "\")'>" +
                    "<i class='lnr lnr-trash'></i>" +
                    "</a>" +
                    "</td>" +
                    "</tr>"
                )

            });

            // pageMe.js 使用方法
            $("#page").paging({
                pageNum: currentPage, // 当前页面
                totalNum: pages, // 总页码
                totalList: total, // 记录总数量
                pageSize: pageSize,
                callback: function (num, size) { //回调函数
                    pageSize = size;
                    currentPage = num;
                    getFollowedList(num, size, search_value);
                    // 返回顶部
                    if ($(window).scrollTop() > 200) {
                        $('body,html').animate({
                            scrollTop: 0
                        });
                    }

                }
            });
        },
        error: function (err) {
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}

//  搜索框
function click_search() {
    var tmp = $('#follows_search').val();
    if (tmp === "" || tmp === null) {
        tmp = "";
    }
    search_value = tmp;
    getFollowedList(currentPage, pageSize, search_value);
}

function companyDetail(companyId) {
    location.href = "/oa/company/companyDetailsPage/" + companyId;
}

function cancelFollowCompany(companyId) {
    $.ajax({
        type: "POST",
        async: false,
        url: companyServer + "/company/cancelFollowCompany",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        // headers: {
        //     "Content-Type": "application/json"
        // },
        data: {
            userId: userId,
            companyId: companyId,
        },
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });
                $("#com-" + companyId).remove();
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}
